package com.geostar.constructionland.common.util;

import java.io.*;

public class FileUtil {

    /**
     * 创建文件
     * @param filePath
     * @param fileContent
     * @return
     */
    public static boolean createFile(String filePath, String fileContent) {
        Boolean result = false;
        //将写入转化为流的形式
        BufferedWriter bw = null;
        try {
            bw = new BufferedWriter(new FileWriter(filePath));
            bw.write(fileContent);
            result = true;
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (bw != null) {
                    bw.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return result;
    }

    /**
     * 删除文件
     * @param filePath
     * @return
     */
    public  static boolean delFile(String filePath) {
        Boolean result = false;
        File file = new File(filePath);
        try {
            if (file.exists()) {
                if(file.isDirectory()) {
                    String[] children = file.list();
                    for (int i = 0; i < children.length; i++) {
                        delFile(filePath + File.separator + children[i]);
                    }
                }
                file.delete();
                result = true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 压缩包转字符串
     * @param filePath
     * @return
     */
    public static String getString(String filePath) {
        StringBuffer result = new StringBuffer();
        File file = new File(filePath);
        FileInputStream fis = null;
        BufferedInputStream bis = null;
        InputStreamReader iReader = null;
        BufferedReader reader = null;
        try {
            fis = new FileInputStream(file);
            bis = new BufferedInputStream(fis);
            iReader = new InputStreamReader(bis, "GBK");
            reader = new BufferedReader(iReader);
            while (reader.ready()) {
                result.append((char)reader.read());
            }
            reader.close();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (reader != null) {
                    reader.close();
                }
                if (iReader != null) {
                    iReader.close();
                }
                if (bis != null) {
                    bis.close();
                }
                if (fis != null) {
                    fis.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return result.toString();
    }

    /**
     * 获得指定文件的byte数组
     * @param filePath
     * @return
     */
    public static byte[] getBytes(String filePath) {
        byte[] buffer = null;
        FileInputStream fis = null;
        ByteArrayOutputStream bos = null;
        try {
            File file = new File(filePath);
            fis = new FileInputStream(file);
            bos = new ByteArrayOutputStream(1000);
            byte[] b = new byte[1000];
            int n;
            while ((n = fis.read(b)) != -1) {
                bos.write(b, 0, n);
            }
            buffer = bos.toByteArray();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (fis != null) {
                    fis.close();
                }
                if (bos != null) {
                    bos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return buffer;
    }

    /**
     * 根据byte数组，生成文件
     * @param bfile
     * @param filePath
     * @param fileName
     */
    public static File getFile(byte[] bfile, String filePath, String fileName) {
        BufferedOutputStream bos = null;
        FileOutputStream fos = null;
        File file = null;
        try {
            File dir = new File(filePath);
            if (!dir.exists() && dir.isDirectory()) {    //判断文件目录是否存在
                dir.mkdirs();
            }
            file = new File(filePath + "\\" + fileName);
            fos = new FileOutputStream(file);
            bos = new BufferedOutputStream(fos);
            bos.write(bfile);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (bos != null) {
                    bos.close();
                }
                if (fos != null) {
                    fos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return file;
    }
}
